package org.jeecg.modules.geo.service.resolve.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.bim.entity.BimDevs;
import org.jeecg.modules.bim.service.IBimDevsService;
import org.jeecg.modules.geo.core.constant.BimGeoEventType;
import org.jeecg.modules.geo.entity.BimGeoEvent;
import org.jeecg.modules.geo.model.dto.DeviceDTO;
import org.jeecg.modules.geo.model.vo.DeviceVO;
import org.jeecg.modules.geo.service.resolve.GeoBaseResolve;
import org.jeecg.modules.geo.service.resolve.GeoBaseService;
import org.jeecg.modules.geo.service.resolve.IGeoResolves;
import org.jeecg.modules.icon.entity.BimGeoIcon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName
 * @Description TODO
 * @Author czd
 * @Date 2021/12/27 9:55
 * @Version 1.0
 **/
@Service
@Slf4j
public class DevsGeoResolveServiceImpl implements IGeoResolves {

    @Autowired
    private IBimDevsService bimDevsService;


    public List<DeviceDTO> resolveDevs(List<DeviceDTO> devices, List<BimGeoEvent> geoEventConfigResults, List<BimGeoIcon> geoIconConfigResults) {
        if (devices == null || (geoEventConfigResults == null && geoIconConfigResults == null)) {
            return devices;
        }
        //event解析
        Map<Integer, Map<Integer, List<BimGeoEvent>>> defaultEventsAll = GeoBaseService.getGeoDefaultEvent(geoEventConfigResults, BimGeoEventType.DEVS_EVENT_TYPE.getValue());
        Map<String, List<BimGeoEvent>> eventsGroupByName = GeoBaseService.getGeoEventGroupbyName(geoEventConfigResults, BimGeoEventType.DEVS_EVENT_TYPE.getValue());
        //icon解析
        Map<Integer, List<BimGeoIcon>> defaultIcons = GeoBaseService.getGeoDefaultIcons(geoIconConfigResults, BimGeoEventType.DEVS_EVENT_TYPE.getValue());
        Map<String, List<BimGeoIcon>> iconGroupByName = GeoBaseService.getGeoIconGroupByName(geoIconConfigResults, BimGeoEventType.DEVS_EVENT_TYPE.getValue());

        //解析鼠标事件
        for (DeviceDTO device : devices) {
            String leftAction = device.getLeftAction();
            HashMap<String, Object> clicks = new HashMap<>();

            if ((leftAction != null && !leftAction.isEmpty() && eventsGroupByName != null) || defaultEventsAll != null) {
                //默认只有一个事件
                Map<String, Object> clickLeft = GeoBaseResolve.resolveLeftEvent(defaultEventsAll, eventsGroupByName, leftAction, device.getParams(), BimGeoEventType.DEVS_EVENT_TYPE.getValue());
                clicks.put("clickLeft", clickLeft);
            }
            String rightAction = device.getRightAction();
            if ((rightAction != null && !rightAction.isEmpty() && eventsGroupByName != null) || defaultEventsAll != null) {
                Map<String, Object> clickRight = GeoBaseResolve.resolveRightEvent(defaultEventsAll, eventsGroupByName, rightAction, device.getParams(), BimGeoEventType.DEVS_EVENT_TYPE.getValue());
                clicks.put("clickRight", clickRight);
            }
            device.setAction(clicks);

            String customIcon = device.getCustomIcon();
            if (defaultIcons != null || (StrUtil.isNotEmpty(customIcon) && iconGroupByName != null)) {
                Map<String, Object> icons = GeoBaseResolve.resolveIcons(defaultIcons, iconGroupByName, customIcon, BimGeoEventType.DEVS_EVENT_TYPE.getValue());
                device.setIcon((HashMap<String, Object>) icons);
            }
        }
        return devices;
    }

    @Override
    public List<DeviceDTO> resolveDevs(List<DeviceDTO> devices, Long projectId) {
        List<BimDevs> bimDevs = bimDevsService.queryDevs(null, projectId);
        if (CollUtil.isEmpty(bimDevs)) {
            log.info("获取bim设备数据,第三方项目id未在系统=>{}中查询到", projectId);
            return devices;
        }
        Map<String, String> bimDevsMap = bimDevs.stream().collect(Collectors.toMap(BimDevs::getName, BimDevs::getDevs));
        //解析鼠标事件
        for (DeviceDTO device : devices) {
            String customIcon = device.getCustomIcon();
            //名称为空,不做处理
            if (StrUtil.isBlank(customIcon))
                continue;
            String devsInfo = bimDevsMap.get(customIcon);
            if (StringUtils.isBlank(devsInfo))
                continue;
            //查询系统配置的样式
            DeviceVO deviceVO = JSON.parseObject(devsInfo, DeviceVO.class);
            if (Objects.nonNull(deviceVO)) {
                //鼠标事件
                device.setAction(deviceVO.getAction());
                //图标信息
                device.setIcon(deviceVO.getIcon());
                //设备经纬度信息
                this.buildLatAndLng(device, deviceVO);
            }
        }
        return devices;
    }


    private void buildLatAndLng(DeviceDTO device, DeviceVO deviceVO) {
        HashMap<String, Object> coordinate = device.getCoordinate();
        String latitude = Convert.toStr(coordinate.get("latitude"));
        String longitude = Convert.toStr(coordinate.get("longitude"));
        if (StrUtil.isBlank(latitude) || StrUtil.isBlank(longitude)) {
            HashMap<String, Object> dtoCoordinate = deviceVO.getCoordinate();
            coordinate.put("latitude", dtoCoordinate.get("latitude"));
            coordinate.put("longitude", dtoCoordinate.get("longitude"));
        }
    }
}
